package editAir.editContent
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	public class ListScroll extends MovieClip
	{
		private var _mcscroll:MovieClip;
		private var _allMc:Sprite
		private var _maskMc:Sprite;
		private var a:MovieClip;
		private var b:MovieClip;
		private var c:MovieClip;

		public function ListScroll(mc:Sprite, mask:Sprite)
		{
			_allMc=mc;
			_maskMc=mask;
			addPics();
			addEvents()

		}
		private var _scroll_move:Boolean=false;
		private var _lastc_y:Number=0;
		private var _lastmouse_y:Number=0;

		public function changeSize():void
		{
			b.y=_maskMc.height - b.height
			var bigH:Number=_allMc.height - _maskMc.height;
			var maxH:Number=_maskMc.height - (2 * b.height);
			if (bigH > 0)
			{
				c.visible=true;
				c.height=maxH * (_maskMc.height / _allMc.height)

				if ((c.y + c.height) > b.y)
				{
					c.y=b.y - c.height;
				}


			}
			else
			{
				c.visible=false;
			}
		}

		public function speedNum(num:Number):void
		{
			var bigH:Number=_allMc.height - _maskMc.height;
			var maxH:Number=_maskMc.height - (2 * b.height);
			if (bigH > 0)
			{
				var moveH:Number=(bigH / 50 * num);

				moveH=Math.abs(moveH) < 6 ? num : moveH * 2;


				var ty:Number=c.y + (bigH / 50 * num)
				ty=ty < a.height ? a.height : ty;
				ty=ty > (b.y - c.height) ? (b.y - c.height) : ty;
				c.y=ty;
				_allMc.y=(c.y - a.height) / (_maskMc.height - a.height - b.height - c.height) * (-bigH)
				_allMc.y=int(_allMc.y);
			}
		}

		public function setMcShowPositionY(num:Number):void
		{
			
			var bigH:Number=_allMc.height - _maskMc.height;
			
		
			var maxH:Number=_maskMc.height - (2 * b.height);

			var toY:Number=-num;
			toY=toY < (_maskMc.height - _allMc.height) ? (_maskMc.height - _allMc.height) : toY;


			var lastY:Number=_allMc.y;
			var itime:uint=setInterval(myRepeatingFunction, 10, "Hello", "World");
			var loopNum:int=0
			function myRepeatingFunction():void
			{
				var sc:Number=easeInOut(loopNum, 0, 1, 20)
				loopNum++
				_allMc.y=lastY + (toY - lastY) * sc;
				_allMc.y=int(_allMc.y)>0?0:int(_allMc.y);


				c.y=_allMc.y / (-bigH) * (_maskMc.height - a.height - b.height - c.height) + a.height
				
				

				if (loopNum >= 20)
				{
					clearInterval(itime);
				}
			}

		}

		private function easeInOut(t:Number, b:Number, c:Number, d:Number):Number
		{
			if ((t/=d / 2) < 1)
			{
				return c / 2 * t * t * t * t + b;
			}
			return -c / 2 * ((t-=2) * t * t * t - 2) + b;
		}

		private function addEvents():void
		{
			c.addEventListener(MouseEvent.MOUSE_DOWN, cMouseDown)
			ModelUiManege.stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp)
			ModelUiManege.stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMove)

		}

		protected function stageMouseMove(event:MouseEvent):void
		{
			if (_scroll_move == true)
			{
				var ty:Number=_lastc_y + (mouseY - _lastmouse_y);
				ty=ty < a.height ? a.height : ty;
				ty=ty > (b.y - c.height) ? (b.y - c.height) : ty;
				c.y=ty;

				var bigH:Number=_allMc.height - _maskMc.height;
				_allMc.y=(c.y - a.height) / (_maskMc.height - a.height - b.height - c.height) * (-bigH)
			}
		}

		protected function stageMouseUp(event:MouseEvent):void
		{
			_scroll_move=false;
		}

		protected function cMouseDown(event:MouseEvent):void
		{
			_scroll_move=true;
			_lastc_y=c.y;
			_lastmouse_y=mouseY;

		}

		private function addPics():void
		{
			_mcscroll=ModelUiManege.getClass("sample_scroll");
			addChild(_mcscroll);

			a=_mcscroll.a;
			b=_mcscroll.b;
			c=_mcscroll.c;
		}
	}
}
